﻿using ShSoft.Common.PoweredByLee;
using SlamDunk.CMS.BLL;
using SlamDunk.CMS.Model.ViewModel;
using SlamDunk.CMS.UI.Controllers;
using System.Web.Mvc;

namespace SlamDunk.CMS.UI.Areas.Admin.Controllers
{
    /// <summary>
    /// 用户控制器（用户部分）
    /// </summary>
    public partial class UserController : BaseController
    {
        #region 00.实例化业务层 && 常量
        /// <summary>
        /// 实例化业务层
        /// </summary>
        private readonly UserBll UserBll = new UserBll();

        /// <summary>
        /// 验证码Session键
        /// </summary>
        private const string ValidCodeKey = "ValidCode";
        #endregion

        #region 01.登录视图
        /// <summary>
        /// 登录视图
        /// </summary>
        /// <returns>视图</returns>
        [AllowAnonymous]
        public ActionResult Login()
        {
            return this.View();
        }
        #endregion

        #region 02.校验用户登录
        /// <summary>
        /// 校验用户登录方法
        /// </summary>
        /// <param name="userView">User视图模型</param>
        /// <returns>JsonModel</returns>
        [AllowAnonymous]
        public ActionResult CheckLogin(UserViewForLogin userView)
        {
            //判断模型验证是否通过
            if (this.ModelState.IsValid)
            {
                //1.校验验证码
                if (this.Session[ValidCodeKey] != null && this.Session[ValidCodeKey].ToString() == userView.ValidCode)        //验证码正确
                {
                    this.Session[ValidCodeKey] = null;       //清空验证码
                    //2.校验用户名密码
                    return OperateContext.Current.CheckLogin(userView);
                }
                else                                  //验证码错误
                {

                    //清空验证码
                    this.Session[ValidCodeKey] = null;       //清空验证码
                    //返回JsonResult
                    return OperateContext.Current.JsonModel(0, "验证码错误！");
                }
            }
            else
            {
                return OperateContext.Current.JsonModel(0, "您提交的数据有误，请重试！");
            }
        }
        #endregion

        #region 03.获取验证码图片
        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns>验证码图片二进制数组</returns>
        [AllowAnonymous]
        public ActionResult GetValidCode()
        {
            string vCode = ValidCodeGenerator.GenerateCode(4);
            byte[] buffer = ValidCodeGenerator.GenerateStream(vCode);
            this.Session[ValidCodeKey] = vCode;
            return this.File(buffer, @"image/jpeg");
        }
        #endregion

        #region 04.注销登录
        /// <summary>
        /// 注销登录
        /// </summary>
        /// <returns>JsonResult</returns>
        public ActionResult Logout()
        {
            //1.清空Session
            OperateContext.Current.Session.Clear();
            return OperateContext.Current.JsonModel(1, "注销成功", "/Admin/User/Login");
        }
        #endregion

        #region 05.修改密码
        /// <summary>
        /// 修改密码方法
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <param name="oldPwd">旧密码</param>
        /// <param name="newPwd">新密码</param>
        /// <returns>修改结果</returns>
        public ActionResult UpdatePassword(int userId, string oldPwd, string newPwd, string newPwd2)
        {
            //1.判断两次密码是否一致
            if (newPwd == newPwd2)
            {
                //修改成功后，强制让用户重新登录
                if (this.UserBll.UpdatePassword(userId, oldPwd, newPwd))
                {
                    OperateContext.Current.Session.Clear();
                    return OperateContext.Current.JsonModel(1, "修改成功，请重新登录！", "/Admin/User/Login");
                }
                else
                {
                    return OperateContext.Current.JsonModel(0, "修改失败，请重试！");
                }
            }
            else
            {
                return OperateContext.Current.JsonModel(0, "两次密码输入不一致，请重试！");
            }
        }
        #endregion
    }
}
